package com.jerry.netty.channel;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Channel01 {
    public static void main(String[] args) throws Exception{

        //创建文件输入流
        File file = new File("file_source.txt");    //file_source.txt在根目录下放着。
        FileInputStream fileInputStream = new FileInputStream(file);

        //通过fileInputStream获取对应的FileChannel
        FileChannel fileChannel = fileInputStream.getChannel();

        //创建缓存区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());

        //将通道中的数据读取出来，放到缓冲区中
        fileChannel.read(byteBuffer);

        //将byteBuffer的字节数据 转化为 String 并输出到控制台
        System.out.println(new String(byteBuffer.array()));

        //关闭输入流
        fileInputStream.close();
    }
}
